package zh.demo.datasource;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by zhangheng on 2017/10/31.
 */
@Aspect
@Component
public class DataSourceAop {
    private static final Logger logger = LoggerFactory.getLogger(DataBaseConfiguration.class);

    @Before("execution(* zh.demo.dao..*.select*(..)) || execution(* zh.demo.dao..*.get*(..))")
    public void setReadDataSourceType() {
        DynamicDataSourceHolder.read();
        logger.info("dataSource切换到：Read");
    }

    @Before("execution(* zh.demo.dao..*.insert*(..)) || execution(* zh.demo.dao..*.update*(..))")
    public void setWriteDataSourceType() {
        //DataSourceContextHolder.write();
        DynamicDataSourceHolder.write();
        logger.info("dataSource切换到：write");
    }
}
